import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { OrderService } from '../../../../providers/order-service';
import { AuthService } from '../../../../providers/auth-service';
import { OrderDetailPage } from '../detail/detail'
import { DictionaryService } from '../../../../providers/dictionary-service';
import { OrderPhotoServices } from '../../../../providers/photo-service';



@Component({
  selector: 'pages-order-scan',
  templateUrl: 'scan.html'
})

export class ScanPage {
  orderList:any
  currentUser:any
  btnText:string
  constructor(
     public navCtrl: NavController,
     private alertCtrl: AlertController,
     private order: OrderService,
     private auth:AuthService,
     private dict: DictionaryService,
     private photo: OrderPhotoServices
    ) {
    this.currentUser  = auth.getUserInfo()
    this.btnText = this.currentUser.factory.type === 0?'发布订单':'浏览订单'
    this.loadAll() 
  }
  public loadAll () {
     this.order.getAll().then(data => {
      data.forEach(item => {
        item.typeName = this.dict.getLableByTypeAndvalue('business', item.type)
        this.photo.getPhotoByOrderId(item.id).then(data => {
          item.photos = data
        })
      })
      this.orderList = data
    })
  }
  public toDetailOrder(order) {
    this.navCtrl.push(OrderDetailPage, {
      order: order
    })
  }
  public recipientOrder(order) {
    this.order.recipientOrder({
      recipient_id: this.currentUser.user.id,
      id: order.id
    }).then(data => {
      this.showPopup('', '订单承接成功')
    })
  }

  showPopup(title, text) {
    let alert = this.alertCtrl.create({
      title: title,
      subTitle: text,
      buttons: [
       {
         text: 'OK',
         handler: data => {
           this.loadAll()
         }
       }
     ]
    });
    alert.present();
  }
}
